var userName;
var password;
var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
    // Create the form
    editor = new $.fn.dataTable.Editor( {
        "ajaxUrl": "php/browsers.php",
        "domTable": "#example",
        "display": 'envelope',
        "fields": [ {
                "label": "Browser:",
                "name": "browser"
            }, {
                "label": "Rendering engine:",
                "name": "engine"
            }, {
                "label": "Platform:",
                "name": "platform"
            }, {
                "label": "Version:",
                "name": "version"
            }, {
                "label": "CSS grade:",
                "name": "grade"
            }
        ]
    } );
 
    // New record
    $('a.editor_create').on('click', function (e) {
        e.preventDefault();
 
        editor.create(
            'Create new record',
            { "label": "Add", "fn": function () { editor.submit(); } }
        );
    } );
 
    // Edit record
    $('#example').on('click', 'a.editor_edit', function (e) {
        e.preventDefault();
 
        editor.edit(
            $(this).parents('tr')[0],
            'Edit record',
            { "label": "Update", "fn": function () { editor.submit(); } }
        );
    } );
 
    // Delete a record
    $('#example').on('click', 'a.editor_remove', function (e) {
        e.preventDefault();
 
        editor.message( "Are you sure you wish to delete this row?" );
        editor.remove(
            $(this).parents('tr')[0],
            'Delete Row',
            { "label": "Delete", "fn": function () { editor.submit(); } }
        );
    } );
 
    // DataTables init
    $('#example').dataTable( {
        "sDom": "Tfrtip",
        "sAjaxSource": "php/browsers.php",
        "aoColumns": [
            { "mData": "browser" },
            { "mData": "engine" },
            { "mData": "platform" },
            { "mData": "grade", "sClass": "center" },
            {
                "mData": null,
                "sClass": "center",
                "sDefaultContent": '<a href="" class="editor_edit">Edit</a> / <a href="" class="editor_remove">Delete</a>'
            }
        ]
    } );
} );

function logIn(){
	userName = document.getElementById("userName").value;
	password = document.getElementById("password").value;
	var boxLogin = document.getElementById("boxLogin");
	var boxWelcome = document.getElementById("boxWelcome");
	var userManager = document.getElementById("menu-item-4");
	
	$.ajax ({
		   type : "POST",
		   url : "/MathDict/login.do",
		   data : {
			   message: "kiem tra dang nhap",
			   userName : userName,
			   password : password
		   },
		   success : function(data) {
			   if (!("error"== data)){ 
				   boxLogin.style.display = 'none';
				   document.getElementById("welcomeName").innerHTML = data;
				   boxWelcome.style.display = '';
				   if("admin"==data){
					   userManager.style.display = '';
				   }
			   }
		   },
		   error : function(e) {
		    alert('Error ' + e);
		   }
		  });
	
}
function logOut1(){
	userName = document.getElementById("userName").value;
	password = document.getElementById("password").value;
	var boxLogin = document.getElementById("boxLogin");
	var boxWelcome = document.getElementById("boxWelcome");
	var userManager = document.getElementById("menu-item-4");
	
	$.ajax ({
		   type : "POST",
		   url : "/MathDict/logout.do",
		   data : {
			   message: "Thoat",
		   },
		   success : function(data) {
			   if (!("error"== data)){ 
				   boxLogin.style.display = '';
				   boxWelcome.style.display = 'none';
				   userManager.style.display = 'none';
			   }
		   },
		   error : function(e) {
		    alert('Error ' + e);
		   }
		  });
	
}
function logOut2(){
	userName = document.getElementById("userName").value;
	password = document.getElementById("password").value;
	var boxLogin = document.getElementById("boxLogin");
	var boxWelcome2 = document.getElementById("boxWelcome2");
	var userManager = document.getElementById("menu-item-4");
	
	$.ajax ({
		   type : "POST",
		   url : "/MathDict/logout.do",
		   data : {
			   message: "Thoat",
		   },
		   success : function(data) {
			   if (!("error"== data)){ 
				   boxLogin.style.display = '';
				   boxWelcome2.style.display = 'none';
				   userManager.style.display = 'none';
			   }
		   },
		   error : function(e) {
		    alert('Error ' + e);
		   }
		  });
	
}

